package com.ruoyi.kycinfo.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kycinfo.domain.*;
import com.ruoyi.kycinfo.dto.KycUserInfoDTO;
import com.ruoyi.kycinfo.dto.KycUserInfoFilterDTO;
import com.ruoyi.kycinfo.mapper.KycMapper;
import com.ruoyi.kycinfo.service.*;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @DESCRIPTION :
 * @date : 2023/10/16 16:20
 */

@Service
public class KycServiceImpl implements IKycService {
    @Autowired
    private ISysUserService SysUserService;

    @Autowired
    private IKycUserInfoService kycUserInfoService;

    @Autowired
    private IKycFamilyService kycFamilyService;

    @Autowired
    private IKycWorkService kycWorkService;

    @Autowired
    private IKycProductService kycProductService;

  /*  @Autowired
    private IKycPotentialService kycPotentialService;

    @Autowired
    private IKycContactService kycContactService;*/

    @Autowired
    private KycMapper kycMapper;

    /*
     查询当前登陆用户能看到的 KycUserInfo客户的 userInfoId列表
   * */
    @Override
    public List<Long> selectKycUserInfoIdList() {
        // 获取当前登陆用户的 userId
        Long curUserId = SecurityUtils.getUserId();
        Set<Long> ids = new HashSet<>();
        ids.add(curUserId);
        // ids 中加入其他支行客户岗员工的userId
        SysUser user = new SysUser();
        List<SysUser> users = SysUserService.selectUserList(user);
        for (SysUser sysUser : users) {
            ids.add(sysUser.getUserId());
        }

        // 根据可以查询到的员工userId列表，查询当前登陆用户可以访问到的kycUsreInfo的userInfoId列表
        List<Long> userInfoIdList = kycMapper.selectKycUserInfoIdListByUserIds(ids);

        return userInfoIdList;
    }


    // 使用事务
    @Transactional
    @Override
    public long insertKycInfo(KycUserInfoDTO kycUserInfoDTO) {
        KycUserInfo kycUserInfo = kycUserInfoDTO.getKycUserInfo();
        Long userInfoId = kycUserInfo.getUserInfoId();

        // 插入1个KycUserInfo对象
        if (userInfoId != null) {
            // "其他"字段 有可能获取到  null
            String clienTypeOther = kycUserInfo.getClienTypeOther();
            if (!StringUtils.isEmpty(clienTypeOther)) {
                String clienType = kycUserInfo.getClienType();
                clienType = clienType + "," + clienTypeOther;
                kycUserInfo.setClienType(clienType);
            }
            kycUserInfoService.insertKycUserInfo(kycUserInfo);
        } else {
            // 如果没有填写客户号，则直接返回，不进行数据保存
            return -1;
        }

        // 插入多个KycFamily对象
        List<KycFamily> kycFamilyList = kycUserInfoDTO.getKycFamilyList();
        for (KycFamily kycFamily : kycFamilyList) {
            if (!StringUtils.isEmpty(kycFamily.getFamilyName())) {
                kycFamily.setUserInfoId(userInfoId);
                kycFamilyService.insertKycFamily(kycFamily);
            }
        }

        // 插入1个KycWork对象
        KycWork kycWork = kycUserInfoDTO.getKycWork();
//        if (!StringUtils.isEmpty(kycWork.getCompanyName())) {
        kycWork.setUserInfoId(userInfoId);
        // 持有签证或曾经去过
        // "其他"字段 有可能获取到  null
        String countryOther = kycWork.getCountryOther();
        if (!StringUtils.isEmpty(countryOther)) {
            String country = kycWork.getCountry();
            country = country + "," + countryOther;
            kycWork.setCountry(country);
        }

        kycWorkService.insertKycWork(kycWork);
//        }

        // 插入1个KycProduct对象
        KycProduct kycProduct = kycUserInfoDTO.getKycProduct();
//        if (!StringUtils.isEmpty(kycProduct.getRisk())) {
        kycProduct.setUserInfoId(userInfoId);

        // 重点交叉产品
        // "其他"字段 有可能获取到  null
        String keyProductOther = kycProduct.getKeyProductOther();
        if (!StringUtils.isEmpty(keyProductOther)) {
            String keyProduct = kycProduct.getKeyProduct();
            keyProduct = keyProduct + "," + keyProductOther;
            kycProduct.setKeyProduct(keyProduct);
        }

        // 在我行贷款原因
        // "其他"字段 有可能获取到  null
        String myLoanReasonOther = kycProduct.getMyLoanReasonOther();
        if (!StringUtils.isEmpty(myLoanReasonOther)) {
            String myLoanReason = kycProduct.getMyLoanReason();
            myLoanReason = myLoanReason + "," + myLoanReasonOther;
            kycProduct.setMyLoanReason(myLoanReason);
        }

        // 在他行贷款原因
        // "其他"字段 有可能获取到  null
        String otherLoanReasonOther = kycProduct.getOtherLoanReasonOther();
        if (!StringUtils.isEmpty(otherLoanReasonOther)) {
            String otherLoanReason = kycProduct.getOtherLoanReason();
            otherLoanReason = otherLoanReason + "," + otherLoanReasonOther;
            kycProduct.setOtherLoanReason(otherLoanReason);
        }

        // 借记卡
        // "其他"字段 有可能获取到  null
        String debitCardTypeOther = kycProduct.getDebitCardTypeOther();
        if (!StringUtils.isEmpty(debitCardTypeOther)) {
            String debitCardType = kycProduct.getDebitCardType();
            debitCardType = debitCardType + "," + debitCardTypeOther;
            kycProduct.setDebitCardType(debitCardType);
        }

        // 存款
        // "其他"字段 有可能获取到  null
        String depositTypeOther = kycProduct.getDepositTypeOther();
        if (!StringUtils.isEmpty(depositTypeOther)) {
            String depositType = kycProduct.getDepositType();
            depositType = depositType + "," + depositTypeOther;
            kycProduct.setDepositType(depositType);
        }


        // 基金
        // "其他"字段 有可能获取到  null
        String fundOther = kycProduct.getFundOther();
        if (!StringUtils.isEmpty(fundOther)) {
            String fund = kycProduct.getFund();
            fund = fund + "," + fundOther;
            kycProduct.setFund(fund);
        }

        // 保险
        // "其他"字段 有可能获取到  null
        String insuranceOther = kycProduct.getInsuranceOther();
        if (!StringUtils.isEmpty(insuranceOther)) {
            String insurance = kycProduct.getInsurance();
            insurance = insurance + "," + insuranceOther;
            kycProduct.setInsurance(insurance);
        }

        // 偏爱活动主题
        // "其他"字段 有可能获取到  null
        String activityOther = kycProduct.getActivityOther();
        if (!StringUtils.isEmpty(activityOther)) {
            String activity = kycProduct.getActivity();
            activity = activity + "," + activityOther;
            kycProduct.setActivity(activity);
        }

        // kycProduct表其他字段比较多，不能直接放在if判断中
        kycProductService.insertKycProduct(kycProduct);
//        }

        return userInfoId;
    }

    @Override
    public List<KycUserInfo> selectKycUserInfoList(KycUserInfoFilterDTO userInfoFilterDTO) {
        return kycMapper.selectKycUserInfoList(userInfoFilterDTO);
    }
}
